package com.zxf.android.mobilesafe.activity;

import com.zxf.android.mobilesafe.R;
import com.zxf.android.mobilesafe.service.WatchDogService;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SettingActivity extends Activity {
	    private CheckBox autoUpdate;
	    private CheckBox cb_lockapp;
	    private SharedPreferences sp;
	    private Editor editor;
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_set);
			sp = getSharedPreferences("config",Context.MODE_PRIVATE);
			editor = sp.edit();
			cb_lockapp = (CheckBox) this.findViewById(R.id.cb_lockapp);
			autoUpdate = (CheckBox) this.findViewById(R.id.autoUpdate);
			
			if(sp.getBoolean("appisLock", false)) {
				cb_lockapp.setChecked(true);
			}else {
				cb_lockapp.setChecked(false);
			}
			
			if(sp.getBoolean("autoUpdate", false)) {
				autoUpdate.setChecked(true);
			}else {
				autoUpdate.setChecked(false);
			}
			autoUpdate.setOnCheckedChangeListener(new OnCheckClick());
			cb_lockapp.setOnCheckedChangeListener(new OnCheckClick());
		}
		
		class OnCheckClick implements OnCheckedChangeListener {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				switch (buttonView.getId()) {
				case R.id.autoUpdate:
					if(isChecked) {
						
						editor.putBoolean("autoUpdate", true);
						editor.commit();
					}else {
						editor.putBoolean("autoUpdate", false);
						editor.commit();
					} 
					break;

				case R.id.cb_lockapp:
					if(isChecked){
						Intent intent = new Intent(SettingActivity.this,WatchDogService.class);
						startService(intent);
						editor.putBoolean("appisLock", true);
						editor.commit();
					}else {
						editor.putBoolean("appisLock", false);
						editor.commit();
					}
					break;
				}
			
				
			}
			
		}
}
